package org.handyc.game.object;

import java.awt.geom.AffineTransform;
import java.math.BigDecimal;

import javax.swing.ImageIcon;

public class GameObject {

	private ImageIcon icon;

	private double x;

	private double y;

	private double relativeX;

	private double relativeY;

	private double width;

	private double height;

	private double centerx;

	private double centery;

	private double angle = 0;

	private AffineTransform affineTransform = new AffineTransform();

	public GameObject(ImageIcon icon) {
		this.width = icon.getIconWidth();
		this.height = icon.getIconHeight();
		this.centerx = this.width / 2;
		this.centery = this.height / 2;
	}

	public GameObject(ImageIcon icon, double x, double y) {
		super();
		this.icon = icon;
		this.x = x;
		this.y = y;
		this.relativeX = x;
		this.relativeY = y;

		this.width = icon.getIconWidth();
		this.height = icon.getIconHeight();
		this.centerx = this.width / 2;
		this.centery = this.height / 2;

	}

	public GameObject(ImageIcon icon, double x, double y, double width, double height) {
		super();
		this.icon = icon;
		this.x = x;
		this.y = y;
		this.relativeX = x;
		this.relativeY = y;

		this.width = width;
		this.height = height;
		this.centerx = this.width / 2;
		this.centery = this.height / 2;
	}

	public GameObject(ImageIcon icon, double x, double y, double width, double height, double centerx, double centery) {
		super();
		this.icon = icon;
		this.x = x;
		this.y = y;
		this.relativeX = x;
		this.relativeY = y;

		this.width = width;
		this.height = height;
		this.centerx = centerx;
		this.centery = centery;
	}

	public ImageIcon getIcon() {
		return icon;
	}

	public void setIcon(ImageIcon icon) {
		this.icon = icon;
	}

	public double getWidth() {
		return width;
	}

	public void setWidth(double width) {
		this.width = width;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}

	public double getCenterx() {
		return centerx;
	}

	public void setCenterx(double centerx) {
		this.centerx = centerx;
	}

	public double getCentery() {
		return centery;
	}

	public void setCentery(double centery) {
		this.centery = centery;
	}

	public AffineTransform getAffineTransform() {

		affineTransform.setTransform(this.width / this.icon.getIconWidth(), 0, 0, this.height / this.icon.getIconHeight(), this.relativeX, this.relativeY);
		affineTransform.rotate(Math.toRadians(angle % 360));
		return affineTransform;

	}

	public void setXy(double x, double y) {
		this.x = x;
		this.y = y;
		this.relativeX = x;
		this.relativeY = y;
	}

	public void moveXy(double x, double y) {
		this.x += x;
		this.y += y;
		this.relativeX += x;
		this.relativeY += y;
	}

	int i = 0;

	public void rotate(double angle) {
		//
		this.angle += angle;

		this.angle = this.angle >= 360 ? this.angle - 360 : this.angle;
		this.angle = this.angle < 0 ? this.angle + 360 : this.angle;

		// 算出中心点偏移直线距离
		double radians = Math.toRadians(this.angle / 2);

		double w = Math.abs(centerx);
		double h = Math.abs(centery);

		double s = Math.sqrt(w * w + h * h) * Math.sin(radians) * 2;

		// 算出x轴角度
		double anglex = 90 - Math.toDegrees(Math.atan2(h, w)) + 90 - this.angle / 2;
		double rx = Math.toRadians(anglex);

		double lx;
		double ly;
		lx = Math.sin(rx) * s;
		ly = Math.cos(rx) * s;

		if (centerx >= 0 && centery <= 0) {
			lx = Math.cos(rx) * s;
			ly = -Math.sin(rx) * s;
		} else if (centerx >= 0 && centery > 0) {
			lx = Math.sin(rx) * s;
			ly = Math.cos(rx) * s;
		} else if (centerx < 0 && centery <= 0) {
			lx = -Math.sin(rx) * s;
			ly = -Math.cos(rx) * s;
		} else {
			lx = -Math.cos(rx) * s;
			ly = Math.sin(rx) * s;
		}

		System.out.println(this.angle);
		System.out.println(anglex);
		System.out.println(lx);
		System.out.println(ly);

		this.relativeX = this.x + lx;
		this.relativeY = this.y + ly;
	}

	public static void main(String... args) {
		// AffineTransform affineTransform = new AffineTransform();
		// affineTransform.setTransform(1, 0, 0, 1, 30, 90);
		// System.out.println(affineTransform.getTranslateX());
		// System.out.println(affineTransform.getTranslateY());
		// affineTransform.rotate(Math.toRadians(30));

		double a = Math.atan2(1, 1.7);
		System.out.println(Math.toDegrees(a));
		// System.out.println(affineTransform.getTranslateX());
		// System.out.println(affineTransform.getTranslateY());

		// double a = Math.sin(Math.toRadians(30));
		// double b = Math.cos(Math.toRadians(90));
		// double c = Math.tan(Math.toRadians(45));
		// System.out.println(new BigDecimal(a).toString());
		// System.out.println(new BigDecimal(b).toBigInteger());
		// System.out.println(new BigDecimal(c).toString());

	}

}
